﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class AgentPage_AgentInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            getUserInfo();
        }
       

    }

    protected void getUserInfo()
    {
        
    }

    protected void Button_ok_Click(object sender, EventArgs e)
    {  
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constr);
        conn.Open();
        string selcmd = "SELECT * FROM Agent WHERE agentID="
            + SqlFormat.ap((string)Session["userId"] )
            + " AND acpassword=" + SqlFormat.ap(TextBox_oldpsw.Text) + ";";

        SqlDataAdapter sqladp = new SqlDataAdapter();

        sqladp.SelectCommand = new SqlCommand(selcmd, conn);

        DataTable dataTable = new DataTable();

        sqladp.Fill(dataTable);

        if (dataTable.Rows.Count!=1)
        {
            Response.Write("<script language=javascript > alert('旧密码不正确！');</script>");
            conn.Close();
            return;
        }

        if (!TextBox_newpsw.Text.ToString().Equals(TextBox_okpsw.Text.ToString()))
        {
            Response.Write("<script language=javascript > alert('两次输入的密码不正确！');</script>");
            conn.Close();
            return;
        }

        string updcmd = "UPDATE AGENT SET acpassword=" + SqlFormat.ap(TextBox_okpsw.Text.ToString())
            + " WHERE agentID="
            + SqlFormat.ap((string)Session["userId"])
            + ";";
        sqladp.UpdateCommand = new SqlCommand(updcmd, conn);

        if (sqladp.UpdateCommand.ExecuteNonQuery()!=1)
        {
            Response.Write("<script language=javascript > alert('修改失败..');</script>");
            conn.Close();
            return;
        }

        TextBox_oldpsw.Text = "";
        TextBox_newpsw.Text = "";
        TextBox_okpsw.Text = "";
        Response.Write("<script language=javascript > alert('密码修改成功，请妥善保管新密码');</script>");

        conn.Close();
    }
    protected void LinkButton_userinfo_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void LinkButton_modifypsw_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }

}